//
//  ViewController.m
//  MyClient
//
//  Created by lin on 16/3/22.
//  Copyright © 2016年 lin. All rights reserved.
//

#import "ViewController.h"

#import "DSConnect.h"
#import "GCDAsyncSocket.h"

@interface ViewController () <DSConnectDelegate>
{
    GCDAsyncSocket *_mySocket;
}

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *sendButton;

- (IBAction)sendText:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    DSConnect *connect = [DSConnect sharedDSConnect];
    [connect setDelegate:self];
    [connect start];
    
    _sendButton.enabled = NO;
}

- (void)connectSuccess:(DSConnect *)connect {
    _sendButton.enabled = connect.connected;
    _mySocket = connect.asyncSocket;
    
    [_mySocket readDataWithTimeout:10.0f tag:0];
}

- (IBAction)sendText:(UIButton *)sender {
    NSLog(@"%@", _textField.text);
    
    NSData *data = [_textField.text dataUsingEncoding:NSUTF8StringEncoding];
    [_mySocket writeData:data withTimeout:1.0f tag:1];
}

@end
